#include "Q_TableViewModel.h"

#include <QDebug>
#include <QIcon>
#include <QDate>
Q_TableViewModel::Q_TableViewModel(QObject* parent)
    : QAbstractTableModel{parent}
{
    /** 表头名称 */
    _header << "编号"
            << "书名"
            << "作者"
            << "ISBN"
            << "库存量"
            << "出版日期"
            << "";


    /** 测试数据 */

    QStringList data0;
    data0 << "001" << "Qt编程艺术" << "张伟"
        << "978-7-04-055632-1" << "35"
        << QString::number(QDate::currentDate().daysTo(QDate(2023,5,20)));

    QStringList data1;
    data1 << "002" << "C++设计模式" << "李明"
        << "978-7-115-48972-3" << "22"
        << QString::number(QDate::currentDate().daysTo(QDate(2024,1,12)));

    QStringList data2;
    data2 << "003" << "图形界面开发" << "王芳"
        << "978-7-302-60104-9" << "18"
        << QString::number(QDate::currentDate().daysTo(QDate(2022,11,30)));

    QStringList data3;
        data3 << "004" << "数据库编程" << "陈强"
        << "978-7-121-41856-7" << "47"
        << QString::number(QDate::currentDate().daysTo(QDate(2024,8,15)));

    QStringList data4;
    data4 << "005" << "算法导论"
          << "托马斯·科尔曼"
          << "978-7-115-48972-4"
          << "45"
          << "2023-06-15";

    QStringList data5;
    data5 << "006" << "深入理解计算机系统"
          << "布莱恩特·奥哈拉"
          << "978-7-302-60104-0"
          << "33"
          << "2024-02-28";

    QStringList data6;
    data6 << "007" << "代码整洁之道"
          << "罗伯特·马丁"
          << "978-7-121-41856-8"
          << "27"
          << "2025-01-10";

    QStringList data7;
    data7 << "008" << "设计模式的艺术"
          << "埃里希·伽玛"
          << "978-7-04-055632-2"
          << "19"
          << "2022-09-22";

    QStringList data8;
    data8 << "009" << "人工智能简史"
          << "尼克·博斯特罗姆"
          << "978-7-5603-8741-5"
          << "51"
          << "2024-11-05";

    QStringList data9;
    data9 << "010" << "计算机网络自顶向下"
          << "詹姆斯·库罗斯"
          << "978-7-302-58934-9"
          << "38"
          << "2023-08-17";

    QStringList data10;
    data10 << "011" << "编译原理"
          << "阿霍·乌尔曼"
          << "978-7-111-32768-3"
          << "22"
          << "2024-05-30";

    QStringList data11;
    data11 << "012" << "数学之美"
          << "吴军"
          << "978-7-115-32697-2"
          << "41"
          << "2025-03-12";

    QStringList data12;
    data12 << "013" << "量子计算导论"
          << "迈克尔·尼尔森"
          << "978-7-5603-9854-1"
          << "16"
          << "2022-12-25";

    QStringList data13;
    data13 << "014" << "区块链技术指南"
          << "安德烈亚斯·安东诺普洛斯"
          << "978-7-302-60123-1"
          << "29"
          << "2024-07-19";

    _dataList.append(data0);
    _dataList.append(data1);
    _dataList.append(data2);
    _dataList.append(data3);
    _dataList.append(data4);
    _dataList.append(data5);
    _dataList.append(data6);
    _dataList.append(data7);
    _dataList.append(data8);
    _dataList.append(data9);
    _dataList.append(data10);
    _dataList.append(data11);
    _dataList.append(data12);
    _dataList.append(data13);

    for(QStringList &itme: _dataList)
    {
        itme.append(QString("测试数据"));
    }

    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/NaightNavigationStar.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/MaVieEnRose.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/CollapsingWorld.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/RainMan.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/DarkForest.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/Light.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/STYXHELIX.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/LASTSTARDUST.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
    _iconList.append(QIcon(QPixmap(":/Resource/Image/Model/RunningInTheDark.jpg").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
}

Q_TableViewModel::~Q_TableViewModel()
{
}

int Q_TableViewModel::rowCount(const QModelIndex& parent) const
{
    return _dataList.count();
}

int Q_TableViewModel::columnCount(const QModelIndex& parent) const
{
    return _header.count();
}

QVariant Q_TableViewModel::data(const QModelIndex& index, int role) const
{
    int row = _dataList.count();
    if (role == Qt::DisplayRole)
    {
        // 列0返回文本
        if (index.column() == 0) {
            return _dataList[index.row() % row][0];
        }
/*
        qDebug() << QString("_dataList[%1][%2]:%3")
                        .arg(index.row())
                        .arg(index.column() - 1)
                        .arg(_dataList[index.row()][index.column() - 1]);
*/
        return _dataList[index.row() % row][index.column()];
    }/*
    else if (role == Qt::DecorationRole && index.column() == 0)
    {
        // return _iconList[index.row() % row];
        // return "<书本封面>";
        return QIcon(QPixmap(":/resource/Icon/table_book.png").scaled(38, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }*/
    else if (role == Qt::DecorationPropertyRole)
    {
        return Qt::AlignCenter;
    }
    else if (role == Qt::TextAlignmentRole /*&& index.column() == 4*/)
    {
        return Qt::AlignCenter;
    }
    return QVariant();
}

QVariant Q_TableViewModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
    {
        return _header[section];
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}
